package kiratest;

import okhttp3.*;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by wangning on 2016/6/29.
 */
public class Handling_authentication {

    private final OkHttpClient client;

    public Handling_authentication() {
        client = new OkHttpClient.Builder()
                .authenticator(new Authenticator() {
                    @Override public Request authenticate(Route route, Response response) throws IOException {
                        System.out.println("Authenticating for response: " + response);
                        System.out.println("Challenges: " + response.challenges());
                        String credential = Credentials.basic("jesse", "password1");
                        return response.request().newBuilder()
                                //.header("Authorization", credential)
                                .build();
                    }
                })
                .build();
    }

    @Test
    public void run() throws Exception {
        Request request = new Request.Builder()
                .url("http://publicobject.com/secrets/hellosecret.txt")
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        System.out.println(response.body().string());
    }
}
